<template>
	<view class="content">
	  <image style="width: 100%;height:220vw;" :src="`${picUrl}/quany.png`"></image>
	 
	  <view class="foot">
	  	<!-- <view v-if="is_pay" @click="add_order()" class="pay_button">
	  		立即支付
	  	</view>
		<view v-else class="pay_button">
			立即支付
		</view> -->
		<image v-if="is_pay" @click="add_order()" :src="`${picUrl}/tiyan.png`" class="t_button"></image>
		<image v-else :src="`${picUrl}/tiyan.png`" class="t_button"></image>
	  </view>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				payment_code:'mini_wxpay',
				wxcode: "",
				is_pay:true
			}
		},
		onLoad(e) {
			
			this.WeiXin = true;
			this.getcode();
			
		},
		onShow() {
			this.is_pay=true;
			// this.getXXX()
			switch (uni.getSystemInfoSync().platform) {
				case 'android':
					this.isIos = false
					break;
				case 'ios':
					// #ifdef APP-PLUS
					this.isIos = true
					// #endif
					break;
			}
			this.leftStyle = 0
			if (uni.getStorageSync("key")) {
				this.token = uni.getStorageSync("key") || "";
				this.member_id = uni.getStorageSync('api_member_id')
				uni.setStorageSync("headPortrait", true)
			} else {
				uni.navigateTo({
					url: "/mainA/login/login"
				})
			}
		},
		methods:{
			getcode() { //小程序获取code
				let that = this
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						that.wxcode = loginRes.code
					}
				});
			},
			add_order(){
				this.is_pay = false;
				let that = this;
				let params = {
					order_from: 5,
				}
				
				this.$util.request({
					url: '/mobile/index.php?act=member_payment&op=addPrivilege',
					method: 'POST',
					data: params
				}).then(res => {
					console.log(res);
					if (res.error_code != 0) {
						this.is_pay=true;
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					} else {
						
						this.pay(res.datas);
					}
				})
			},
			pay(pay_sn) {
				let that = this;
				let params = {
					payment_code: this.payment_code, //支付方式代号
					wx_code: this.wxcode, //微信code
					openid: uni.getStorageSync('openid') || "",
					pay_sn:pay_sn,
				}
				this.$util.request({
					url: '/mobile/index.php?act=member_payment&op=privilege',
					method: 'POST',
					data: params
				}).then(res => {
					console.log(res);
					if (res.error_code != 0) {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					} else {
						this.TripartitePay(res);
					}
				})
			},
			TripartitePay(res){
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: res.datas.html.timeStamp + '',
					nonceStr: res.datas.html.nonceStr,
					package: res.datas.html.package,
					signType: res.datas.html.signType,
					paySign: res.datas.html.paySign,
					success: function(e) {
						uni.showToast({
							title: "支付成功",
							icon: 'none',
							success() {
								uni.navigateTo({
									url: '/operation/vip/vip'
								})
							}
						});
					},
					fail: function(err) {
						// uni.showModal({
						// 	content: "支付失败 ",
						// 	showCancel: false,
						// })
						setTimeout(() => {
							uni.navigateTo({
								url: '/operation/vip/vip'
							})
						},500)
						// console.log('fail:' + JSON.stringify(err));
					}
				});
			}
		}
	}
</script>

<style>
	.foot{
		    display: flex;  
		    align-items: center;  
		    justify-content: center; 
	}
	
	.pay_button {
			position: fixed;
			bottom: 0;
		    width: 320rpx;
		        height: 75rpx;
		        font-size: 30rpx;
		        color: #fff;
		        background: #ff7d14;
		        border-radius: 40px;
		        line-height: 75rpx;
		        text-align: center;
	}
	.t_button {
			position: fixed;
			bottom: 46rpx;
		    width: 86%;
		    height: 140rpx;
		       
	}
</style>